package com.ying.excel.service.impl;

import com.ying.excel.domain.po.ExecelUser;
import com.ying.excel.domain.vo.BusinessDataVO;
import com.ying.excel.mapper.ExecelUserMapper;
import com.ying.excel.service.IExecelUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-10-09
 */
@Service
public class ExecelUserServiceImpl extends ServiceImpl<ExecelUserMapper, ExecelUser> implements IExecelUserService {

    @Override
    public void exportBusinessData(HttpServletResponse response) {

        //1. 查询数据库，获取营业数据---查询最近30天的运营数据
        LocalDate dateBegin = LocalDate.now().minusDays(30);
        LocalDate dateEnd = LocalDate.now().minusDays(1);

        //查询概览数据

        //2. 通过POI将数据写入到Excel文件中
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/excel/运营数据报表模板.xlsx");

        try {
            //基于模板文件创建一个新的Excel文件
            XSSFWorkbook excel = new XSSFWorkbook(in);

            //获取表格文件的Sheet页
            XSSFSheet sheet = excel.getSheet("Sheet1");

            //填充数据--时间
            sheet.getRow(1).getCell(1).setCellValue("时间：" + dateBegin + "至" + dateEnd);

            //获得第4行
            XSSFRow row = sheet.getRow(3);
            row.getCell(2).setCellValue(1);
            row.getCell(4).setCellValue(2);
            row.getCell(6).setCellValue(3);

            //获得第5行
            row = sheet.getRow(4);
            row.getCell(2).setCellValue(3);
            row.getCell(4).setCellValue(3);

            //填充明细数据
            for (int i = 0; i < 30; i++) {
                LocalDate date = dateBegin.plusDays(i);
                //查询某一天的营业数据

                //获得某一行
                row = sheet.getRow(7 + i);
                row.getCell(1).setCellValue(date.toString());
                row.getCell(2).setCellValue(0);
                row.getCell(3).setCellValue(0);
                row.getCell(4).setCellValue(1);
                row.getCell(5).setCellValue(3);
                row.getCell(6).setCellValue(33);
            }

            //3. 通过输出流将Excel文件下载到客户端浏览器
            ServletOutputStream out = response.getOutputStream();
            // 设置响应头
            String filename = "export.xlsx";
            response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
            response.setStatus(HttpServletResponse.SC_OK);
            excel.write(out);
            //关闭资源
            out.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
